import { queryTicketInspectList } from '@/api/spw/ticketInspect.js'
import { actNodePermissionEnum } from '@/utils/constant.js'
import { mapGetters } from 'vuex'
import { poiValid } from '../../../api/spw/project'

export default {
  data() {
    return {
      textColor: true,
      listData: [],
      isFinished: false,
      loading: false,
      pageNum: 1,
      total: null
    }
  },
  computed: {
    ...mapGetters(['staffId']),
    isEmpty() {
      return this.isFinished && !this.listData.length
    }
  },
  beforeMount() {
    this.getList()
  },
  methods: {
    styleSelector(option) {
      return 'level_' + option
    },
    initData() {
      this.pageNum = 1
      this.total = null
      this.getList()
    },
    reloadData() {
      this.isFinished = false
      this.loading = false
      this.initData()
    },
    /**
     * @description 校验定位
     */
    async positionValidate(ticketId) {
      // true 不可以处理
      // false 可以处理
      /*
       * 0 未定义围栏 1 已到排查区域 4 忽略校验
       * */
      let { data } = await poiValid(ticketId)
      if (data.code === 0 || data.code === 1 || data.code === 4) {
        return false
      } else {
        this.$modal.showToast(data.title)
        return true
      }
    },
    /**
     * @description 根据不同的节点类型跳转对应的目标页面
     */
    async goPage(row, type, btnLabel) {
      let rew = null
      if (btnLabel !== '分析') {
        rew = await this.positionValidate(row.ticketId)
      }

      if (rew) return

      let url = ''
      switch (type) {
        case 'check':
          url = '/pages/jobSlip/check/inspect'
          break
        case 'start':
          url = '/pages/jobSlip/spw-status/start'
          break
        case 'pause':
          url = '/pages/jobSlip/spw-status/pause'
          break
        default:
          break
      }
      uni.navigateTo({
        url: `${url}?nodeTaskId=${row.nodeTaskId}&ticketId=${row.ticketId}`,
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          'callback:success': (data) => {
            if (data) {
              this.reloadData()
            }
          }
        }
      })
    },
    async getList() {
      if (this.isFinished || this.loading) {
        return
      }
      this.loading = true
      const { rows, total } = await queryTicketInspectList({
        pageNum: this.pageNum,
        listType: this.listType
      }).finally(() => {
        this.loading = false
      })

      rows.map((item) => {
        const guardianIds = item.guardianIds?.split(',') ?? []
        const nodePermissions = item.nodePermissions?.split(',') ?? []
        // 当前节点必须有暂停权限，监护人和节点审查人才能暂停和启动
        if (
          nodePermissions.includes(actNodePermissionEnum.PAUSE) &&
          (guardianIds.includes(String(this.staffId)) || item.assignee === this.staffId)
        ) {
          item.canPauseOrStart = true
        }
        return item
      })

      this.listData = this.pageNum === 1 ? rows : [...this.listData, ...rows]
      this.isFinished = this.listData.length >= total
      this.pageNum += 1
      uni.stopPullDownRefresh()
    }
  }
}
